import React from 'react';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import {FORM_OPTIONS, FORM_ROW_OPTIONS} from '../constants';
import {PERMISSION} from './constants';
import Export from '../common/Export';
import {injectIntl, language} from './intl';
import WrappedSelect from '../common/WrappedSelect';
import WrappedDatePicker from '../common/WrappedDatePicker';
import DateRangePicker from 'Shared/components/DateRangePicker';
import TagSelect from 'Shared/components/TagSelect';
import TextArea from 'Shared/components/TextArea';
import {currency, partsPriceType} from '../Enum';
import {hasPermissions} from './utils';
const FormItem = Form.Item;
const partsPriceTypeList = partsPriceType.toList(language);
const currencyList = currency.toList(language);
class QueryPanel extends React.PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    render() {
        return (
            <Form className="form-standard">
                <Row>
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.brandId',
                                defaultMessage: '品牌'
                            })}
                            {...FORM_OPTIONS.item}>
                            <WrappedSelect
                                allowClear
                                name="brandId"
                                value={this.props.conditions.brandId}
                                options={this.props.brands}
                                onChange={this.handleFilterChange} />
                        </FormItem>
                    </Col>
                    {!this.props.isDealerUser &&
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.dealerCode',
                                    defaultMessage: '总代号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextArea
                                    name="dealerCode"
                                    placeholder={this.props.intl.formatMessage({
                                        id: 'deliveryOrderSelectPanel.form.dealerCode.placeholder',
                                        defaultMessage: '最多输入1000个，以回车隔开'
                                    })}
                                    value={this.props.conditions.dealerCode}
                                    onBlur={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                    }
                    <Col {...FORM_OPTIONS.col}>
                        <FormItem
                            label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.partCodes',
                                defaultMessage: '备件号'
                            })}
                            {...FORM_OPTIONS.item}>
                            <TextArea
                                name="partCodes"
                                placeholder={this.props.intl.formatMessage({
                                    id: 'deliveryOrderSelectPanel.form.partCodes.placeholder',
                                    defaultMessage: '最多输入1000个，以回车隔开'
                                })}
                                value={this.props.conditions.partCodes}
                                onBlur={this.handleFilterChange}/>
                        </FormItem>
                    </Col>
                    {!this.props.isDealerUser &&
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.currency',
                                    defaultMessage: '币种'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="currency"
                                    value={this.props.conditions.currency}
                                    options={currencyList}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                    }
                    {!this.props.isDealerUser &&
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.beginDate',
                                    defaultMessage: '生效日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedDatePicker
                                    name="beginDate"
                                    value={this.props.conditions.beginDate}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                    }
                    
                    {!this.props.isDealerUser &&
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'queryPanel.form.endDate',
                                    defaultMessage: '失效日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedDatePicker
                                    name="endDate"
                                    value={this.props.conditions.endDate}
                                    onChange={this.handleFilterChange}/>
                            </FormItem>
                        </Col>
                    }
                </Row>
                {!this.props.isDealerUser &&
                    <Row>
                        <Col>
                            <FormItem label={this.props.intl.formatMessage({
                                id: 'queryPanel.form.type',
                                defaultMessage: '价格类型'
                            })} {...FORM_ROW_OPTIONS.item}>
                                <TagSelect
                                    name="type"
                                    value={this.props.conditions.type}
                                    options={partsPriceTypeList}
                                    onChange={this.handleFilterChange} />
                            </FormItem>
                        </Col>
                    </Row>
                }
                <Row className="operation-buttons">
                    <Col span={6}>
                        <Button key="query" type="primary" onClick={this.props.querySearch} loading={this.props.loading}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.query',
                                defaultMessage: '查询'
                            })}
                        </Button>
                        <Button key="clear" onClick={this.props.onResetClick}>
                            {this.props.intl.formatMessage({
                                id: 'queryPanel.btn.reset',
                                defaultMessage: '重置'
                            })}
                        </Button>
                    </Col>
                    <Col span={18} className="col-align-right">
                        {hasPermissions(this.props.permissions, PERMISSION.export) &&
                            <Export key={PERMISSION.export} exportRequest={this.props.exportData} />
                        }
                        
                    </Col>
                </Row>
               
            </Form>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    brands: PropTypes.array,
    exportData: PropTypes.func,
    intl: PropTypes.object,
    isDealerUser: PropTypes.bool,
};

import {connect} from 'react-redux';
import {
    saveQueryCondition,
    resetQueryPanel,
    querySearch,
    exportData
} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
import {createSelector} from 'reselect';

const getInitData = createSelector(
    state => state.getIn(['page', 'domainData', 'initData']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? initData.brands.map(item => ({
            text: item.code,
            value: item.id
        })) : [];
        return brands;
    }
);

const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const getInitEnterprise = selectorFactory(['page', 'domainData', 'initData', 'enterprise']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    loading: state.getIn((['page', 'domainData', 'list', 'isFetching'])),
    enterprise: getInitEnterprise(state),
    isDealerUser: state.getIn((['page', 'domainData', 'initData', 'isDealerUser'])),
    conditions: getConditions(state),
    brands: getInitData(state),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    exportData: () => dispatch(exportData()),
    onResetClick: () => dispatch(resetQueryPanel()),
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
